package com.xywl.escort.view.pwd

import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.trello.rxlifecycle2.LifecycleTransformer
import com.xywl.escort.R
import com.xywl.escort.base.BaseActivity
import com.xywl.escort.bean.BaseResp
import com.xywl.escort.bean.MessageEvent
import com.xywl.escort.bean.SetPayPwdReq
import com.xywl.escort.common.Constant
import com.xywl.escort.common.PreferenceKeys.USER_ID
import com.xywl.escort.utils.MD5
import com.xywl.escort.utils.RxSPTool
import com.xywl.escort.utils.ToastUtil
import dagger.android.AndroidInjection
import kotlinx.android.synthetic.main.activity_setting_pay_pwd.*
import org.greenrobot.eventbus.EventBus
import javax.inject.Inject

@Route(path = "/xy/pwd/confirm/pay")
class ConfirmPayPwdActivity : BaseActivity(), ConfirmPayPwdContract.View {
    @Autowired
    @JvmField
    var pwd = ""

    @Inject
    lateinit var mPresenter: ConfirmPayPwdContract.Presenter

    override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_setting_pay_pwd)
        ARouter.getInstance().inject(this)
        showBackButton()
        setTitleText(R.string.setting_set_pay_pwd)

        text_pay_pwd.text = getString(R.string.setting_confirm_pay_pwd)
//        edit_pwd.setOnPasswordChangeListener { password ->
//            if (password.length == 6) {
//                Toast.makeText(this@ConfirmPayPwdActivity, "输入完成: $password", Toast.LENGTH_SHORT).show()
//            }
//        }

        btn_next.text = getString(R.string.setting_confirm)

        mPresenter.takeView(this)
        btn_next.setOnClickListener {
            val newPwd = edit_pwd.text.toString().trim()
            if (pwd == newPwd) {
                val req = SetPayPwdReq()
                req.userId = RxSPTool.getString(this, USER_ID)
                req.payPassword = MD5.encrypt(newPwd)
                mPresenter.addUserPayPwd(req)
            } else {
                ToastUtil.showTip(this, "密码输入不一致")
            }
        }
    }

    override fun isActive(): Boolean {
        return false
    }

    override fun <T : Any?> bindToLife(): LifecycleTransformer<T> {
        return this.bindToLifecycle()
    }

    override fun addUserPayPwdSuccess(resp: BaseResp?) {
        ToastUtil.showTip(this, "设置支付密码成功！！！")

        EventBus.getDefault().post(MessageEvent(Constant.EventType.EVENT_SET_PAY_PWD_TYPE, Constant.EventMessage.FINISH_ACTIVITY))
        finish()
    }

    override fun addUserPayPwdFailure(msg: String?) {
        ToastUtil.showTip(this, msg)
    }
}
